Part Number Hot Search : 
RL153 B1453 ADM3490 D4148 15010 U08A50 K1112 R6545E
Product Description
Full Text Search
 

To Download AN1218 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  AN1218 rev. 2 order this document by AN1218/d rev. 2 AN1218 hc05 to hc08 optimization by mark glenewinkel csic applications austin, texas introduction freescale's hc05 family of microcontrollers contains the world's most popular 8-bit microcontroller units (mcus). in keeping pace with technology and the changing needs of the customer, freescale has designed the hc08 family of mcus. the hc08 family cpu is a performance extension to the hc05 family of low cost mcus. this application note will describe the differences and advantages of the hc08 family cpu: the cpu08. cpu08 is fully opcode and object code compatible with the hc05 cpu. any hc05 code will execute directly on the hc08 without instruction set differences. as this application note will show, there are many improvements to the speed and capability in the cpu08. cpu08 is a faster processor. the basic execution speed of the cpu08 has been increased with advanced high performance cmos technology. execution cycles of most instructions have been improved with an advanced computer architecture. cpu08 has more programming capability. it has more addressing modes, better math support, and much improved data manipulation, accessing, and moving capabilities. looping and branching instructions have also been optimized. f r e e s c a l e s e m i c o n d u c t o r , i f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 2 this application note will help inform and educate the reader concerning the differences between the hc05 and hc08 cpus. detailed examples illustrating the added features found with the cpu08 are given to help optimize software design with the cpu08. scope of this application note this note assumes the reader has a background in mcu software and hardware design and is also familiar with the hc05. it was written for the engineering manager and the design engineer. as a reference, the application note overviews the basic differences between the two cpus so that one can fit the right cpu for a specific application. as a tutorial, the application note gives the designer the means to understand and utilize the hc08 enhancements. software is given to illustrate and compare the performance of the cpus. hc08 features the following is a list of major features of the hc08 cpu (cpu08) that differentiate it from the hc05 cpu (cpu05). ? fully upward object code compatible with the mc6805, mc146805, and the mc68hc05 family ? 64 kbyte program/data memory space ? enhanced hc05 programming model ? 8 mhz cpu bus frequency ? 16 addressing modes, 5 more than the hc05 ? expandable internal bus definition for addressing range extension beyond 64 kbytes ? 16-bit index register with manipulation instructions ? 16-bit stack pointer with manipulation instructions ? memory to memory data moves without using the accumulator f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note cpu05/cpu08 programmer's model comparison AN1218 rev. 2 3 ? fast 8-bit multiply and integer/fractional divide instructions ? binary coded decimal (bcd) instruction enhancements ? internal bus flexibility to accommodate cpu enhancing peripherals such as a dma controller ? fully static low voltage/low power design cpu05/cpu08 programmer's model comparison the cpu05 and the cpu08 programmer's model differences are illustrated in figure 1 . h index register the index register of the cpu08 has been extended to 16 bits, allowing the user to index or address a 64 kbyte memory space without any offset. the upper byte of the index register is called the h index register. the concatenated 16-bit register is called the h:x register. source code written for cpu05 will not affect the h register and it will remain in its reset state of $00. there are seven new instructions that allow the user to manipulate the h:x index register. these instructions are covered in detail later. stack pointer the stack pointer (sp) has been extended from its 6-bit cpu05 version to a full 16-bit sp on the cpu08. sph:spl refers to the 16-bit stack pointer by naming the high byte, sph, and the low byte, spl. to maintain hc05 compatibility, the reset state is $00ff. new instructions and new addressing modes greatly increase the utility of the cpu08 stack pointer over the cpu05 stack pointer. nine new cpu08 instructions allow the user to easily manipulate the sp and the stack. cpu08 also has relative addressing modes that allow the sp to be used as an index register to access temporary variables on the stack. these addressing modes and new instructions are discussed later in this application note. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 4 figure 1. cpu05 and cpu08 comparison program counter expanded the cpu08 program counter (pc) has been expanded to 16 bits which allows the cpu08 to address 64 kbytes of memory. not all hc05 devices have a 16-bit program counter. new addressing modes, comparison cpu08 has 16 addressing modes, 8 more than the hc05. table 1 lists these addressing modes and the cpus that use them. a brief discussion of these modes is given below. a 70 x 70 hinzc ccr 11 sp 7 0 pc 12 0 accumulator index register program counter stack pointer condition code register 0 0 0 0 0 12 accumulator (a) index register (h:x) stack pointer (sp) program counter (pc) condition code register (ccr) v11hinzc h x 0 0 0 0 7 15 15 15 70 (a) cpu05 (b) cpu08 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note cpu05/cpu08 programmer's model comparison AN1218 rev. 2 5 hc05 and hc08 addressing modes inherent instructions such as reset stack pointer (rsp) and multiply (mul) have no operand. inherent instructions require no memory address and are one byte long. immediate instructions contain a value that is used in an operation with the index register or accumulator. immediate instructions require no memory address and are two bytes long. the operand is found in the byte immediately following the opcode. direct instructions can access any of the first 256 memory addresses with only two bytes. the first byte contains the opcode followed by the low byte of the operand address. the cpu automatically uses $00 for the high byte of the operand address. most direct instructions are two bytes long. table 1. addressing mode comparison table addressing mode hc05 hc08 inherent x x immediate x x direct x x extended x x indexed, no offset x x indexed, 8-bit offset x x indexed, 16-bit offset x x relative x x stack pointer, 8-bit offset x stack pointer, 16-bit offset x memory to memory (4 modes) x indexed w/post increment x indexed, 8-bit offset, w/post increment x f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 6 extended instructions can access any address in the memory map. extended instructions are three bytes long and contain the opcode and the two-byte operand address. indexed instructions with no offset are one-byte instructions that utilize the index register of the cpu. cpu08 also uses the h:x register containing the high byte of the address operand. indexed, 8-bit offset instructions are two-byte instructions that utilize the index register of the cpu to access data at any location in memory. the 8-bit unsigned offset following the opcode is added to the 16-bit unsigned index register (h:x). the sum is the address used to access data. indexed, 16-bit offset instructions are like the 8-bit offset instructions except that they are three bytes long and add a 16-bit unsigned number to the 16-bit index register (h:x). relative addressing is only used for branch instructions. if the branching condition is true, the cpu finds the branch destination by adding the offset operand to the pc counter. the offset is a two's complement byte that gives a branching range of C128 to +127 bytes. this instruction is two bytes long. new hc08 addressing modes stack pointer, 8-bit offset instructions operate like indexed, 8-bit offset instructions except that they add the offset to the 16-bit sp. this mode is available only on the cpu08. if interrupts are disabled, this addressing mode allows the sp to be used as a second index register. this instruction is three bytes long. stack pointer, 16-bit offset instructions are only available on the cpu08. they are like the stack pointer, 8-bit offset instructions except that they add a 16-bit value to the sp. this instruction is four bytes long. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note cpu05/cpu08 programmer's model comparison AN1218 rev. 2 7 memory to memory instructions utilize four different modes available only to the cpu08. 1. the move, immediate to direct, is a three-byte mode generally used to initialize ram and register values in page 0 of the memory map. the operand in the second byte is immediately stored to the direct page location found in the third byte. 2. the move, direct to direct, is a three-byte instruction. the operand following the opcode is the direct page location that is stored to the second operand direct page location. 3. the move, indexed to direct, post increment, is a two-byte instruction. the operand addressed by the 16-bit index register (h:x) is stored to direct page location address by the byte following the opcode. the index register is then incremented. 4. the move, direct to indexed, post increment, is a two-byte instruction. the operand in the direct page location addressed by the byte following the opcode is stored in the location addressed by the 16-bit index register (h:x). the index register is then incremented. in the cpu08, four instructions address operands with the index register and then increment the index register afterwards. this is called indexed with post increment mode. these instructions include cbeq indexed, cbeq indexed with offset, mov ix+dir, and mov dirix+. table 2 gives examples to illustrate these different addressing modes. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 8 condition code register with overflow bit v a summary of the condition code register (ccr) is given below. unless otherwise stated, all bits correspond to both cpus. overflow bit v this bit is set when a two's-complement overflow has occurred as the result of an operation. the v bit has been added to the cpu08 condition code register to support two's-complement arithmetic. half-carry bit h the half-carry bit is set when a carry has occurred between bits 3 and 4 of the accumulator because of the last add or adc operation. this bit is required for bcd operations. table 2. addressing mode examples addressing mode example inherent rsp immediate lda #$ff direct lda $50 extended lda $1000 indexed, no offset lda ,x indexed, 8-bit offset lda $50,x indexed, 16-bit offset lda $0150,x relative bra $20 stack pointer, 8-bit offset * lda $50,sp stack pointer, 16-bit offset * lda $0150,sp memory to memory immdir * dirdir * ix+dir * dirix+ * mov mov mov mov #$30,$80 $80,$90 x+,$90 $80,x+ indexed w/post increment * cbeq x+,loop indexed, 8-bit offset, w/post increment * cbeq $20,x+,loop * new cpu08 addressing modes f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note description of the clock AN1218 rev. 2 9 interrupt mask bit i all timer and external interrupts are disabled when this bit is set. interrupts are enabled when the bit is cleared. this bit is automatically set after any cpu reset. negative bit n this bit is set after any arithmetic, logical, or data manipulation operation was negative. in other words, bit 7 of the result of the operation was a logical one. zero bit z the zero bit is set after any arithmetic, logical, or data manipulation operation was zero. carry/borrow bit c the carry/borrow bit is set when a carry out of bit 7 of the accumulator occurred during the last arithmetic, logical, or data manipulation operation. the bit is also set or cleared during bit test and branch instructions and shifts and rotates. description of the clock in the cpu08, the cpu clock rate is twice that of the address/data bus rates. the internal cpu08 clock rate is 16 mhz for an 8 mhz hc08. to maintain a 50% duty cycle cpu clock, the oscillator clock, osc clk, must run twice the rate of the cpu clock. therefore a 32 mhz osc clock is needed to drive an 8 mhz hc08. the flagship member of the cpu08 family has a phase locked loop (pll) synthesizer to generate the 32 mhz signal. it is derived from a suggested crystal frequency of 4.9152 mhz. address/data rate = z = 8 mhz cpu clock rate = 2z = 16 mhz osc clock rate = 4z = 32 mhz f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 10 index registers cpu08 has the additional h index register which is the high byte extension to the x index register. together, the two index registers formulate the concatenated 16-bit h:x index register. five new instructions are introduced on the cpu08 to allow manipulation of the h:x index register. source code written for the hc05 will not effect the h register and it will remain in its reset state of $00. the tsx and the txs instructions also utilize the h:x index register. these instructions are covered in more detail in the stack pointer section. five new indexing instructions, detail the new cpu08 instructions that affect the index registers are listed below. examples for these instructions are given in appendi xanew cpu08 indexing instruction examples . aix add immediate to index register operation: x ? (h:x) + (m) description: aix adds an immediate value to the 16-bit index register formed by the concatenation of the h and x registers. the immediate operand is an 8-bit two's complement signed offset. prior to addition to h:x, the offset is sign extended to 16 bits. clrh clear index high operation: h ? $00 description: the contents of h are replaced with zeros. cphx compare 16-bit index register operation: (h:x) C (m:m+1) description: cphx compares the 16-bit index register h:x with the 16-bit value in memory and sets the condition code register accordingly. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note index registers AN1218 rev. 2 11 ldhx load 16-bit index register operation: h:x ? (m:m+1) description: loads the contents of the speci ed memory location into the 16-bit index register h:x. the condition codes are set according to the data. sthx store 16-bit index register operation: (m:m+1) ? (h:x) description: stores the 16-bit index register h:x to the speci ed memor y location. the condition codes are set according to the data. software techniques using indexed addressing, tables the cpu08 index register has some distinct advantages over the cpu05 index register. even though the cpu05 has 16-bit index offset, the 8-bit index register restricts indexing to a maximum of 256 bytes. cpu08 with its h register extension allows full 16-bit index addressing equaling 65,536 bytes of memory access. proper 16-bit pointers allow efficient compiling of c code and other higher level languages. maximum table lengths in the cpu08 which can be accessed in a single instruction are therefore 64 kbyte. an optional address extension module can extend the data space beyond 64 kbytes, but the maximum offset remains 64 kbytes. index addressing modes include 8- and 16- bit offsets. many programmers like to use calculated addressing. cpu08 has a new instruction, aix, that allows the addition of a two's complement number. table access is easier and more flexible. the h:x index register can also be used as an auxiliary 16-bit accumulator. sixteen-bit data comparisons are easier with the cphx instruction. the following section illustrates the advantage of using a 16-bit index register. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 12 code example we will now illustrate the added benefit of the cpu08 16-bit index register. the index will be used to address a 512 byte table. in the cpu05, the table must be broken up into sections of memory consisting of 256 bytes per section. our table has 512 bytes, so we will be using two sections, section 0 and section 1, for the cpu05. the address to look up on the table will be found in ram. notice that the cpu05 code is longer. if your table was larger, you would require more sections of memory to handle your table. a subroutine might be written to make the job more modular. in the hc08 example, the 512 byte table can be handled directly. a comparison between cpu05 and cpu08 code is shown in appendix b cpu05 and cpu08 512-byte table indexing code . stack pointer cpu08 has a full 16-bit stack pointer. to maintain compatibility with the cpu05, it is initialized to $00ff out of reset. stack manipulation is from high to low memory. the sp is decremented each time data is pushed on the stack and incremented each time data is pulled from the stack. the sp points to the next available stack address rather than the latest stack entry address. nine new instructions have been added for the user to manipulate the stack. these instructions allow the direct push and pull of any register to the stack. the sp can be changed with a transfer of the h:x register to the sp or the sp can be augmented by the add immediate instruction. stack manipulation can be a very powerful programming technique. with the cpu08, the assembly programmer can pass parameters and store local or temporary variables when using subroutines and/or interrupts. new addressing modes were added to address these variables on the stack. using the stack pointer as an index register with 8- or 16-bit offsets, the user may access variables on the stack. these instructions greatly cut cycle count by not having to load/store the variable. ram f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note stack pointer AN1218 rev. 2 13 requirements are also reduced. significant c code efficiency can be gained when utilizing these new stack pointer addressing modes. if interrupts are disabled, the stack pointer can be used as a second 16-bit index register with 8- or 16-bit offsets. nine new stack manipulation instructions, detail all the new cpu08 instructions that affect the stack pointer are listed below. examples for these instructions are given in appendi xcnew cpu08 stack pointer instructions . ais add immediate to stack pointer operation: sp ? (sp) + (m) description: adds the immediate operand to the stack pointer sp. the immediate value is an 8-bit two's complement signed operand. prior to addition to the sp, the operand is sign extended to 16 bits. this instruction can be used to create and remove a stack frame buffer which is used to store temporary variables. psha push accumulator onto stack operation: ? (a); sp ? (sp-$01) description: the contents of the accumulator are pushed onto the stack at the address contained in the stack pointer. the stack pointer is then decremented to point at the next available location in the stack. the contents of the accumulator remain unchanged. pshh push index register h onto stack operation: ? (h); sp ? (sp-$01) description: the contents of the 8-bit high order index register h are pushed onto the stack at the address contained in the stack pointer. the stack pointer is then decremented to point at the next available location in the stack. the contents of the h register remain unchanged. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 14 pshx push index register x onto stack operation: ? (x); sp ? (sp-$01) description: the contents of the 8-bit low order index register x are pushed onto the stack at the address contained in the stack pointer. the stack pointer is then decremented to point at the next available location in the stack. the contents of the x register remain unchanged. pula pull accumulator from stack operation: sp ? (sp+$01); y (a) description: the stack pointer is incremented to address the last operand on the stack. the accumulator is then loaded with the contents of the address pointed to by sp. pulh pull index register h from stack operation: sp ? (sp+$01); y (h) description: the stack pointer is incremented to address the last operand on the stack. the 8-bit index register h is then loaded with the contents of the address pointed to by sp. pulx pull index register x from stack operation: sp ? (sp+$01); y (x) description: the stack pointer is incremented to address the last operand on the stack. the 8-bit index register x is then loaded with the contents of the address pointed to by sp. tsx transfer stack pointer to index register operation: h:x ? (sp) + $0001 description: loads the index register h:x with one plus the contents of the 16-bit stack pointer sp. the contents of the stack pointer remain unchanged. after a tsx instruction, the f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note stack pointer AN1218 rev. 2 15 index register h:x points to the last value that was stored on the stack. txs transfer index register to stack pointer operation: sp ? (h:x) - $0001 description: loads the stack pointer sp with the contents of the index register h:x minus one. the contents of the index register h:x remain unchanged. software techniques using the sp the cpu05 and the cpu08 use the stack for two primary purposes. first, every time the cpu executes an interrupt service routine, the register contents are saved on the stack. after the execution of a return from interrupt (rti) instruction, the register contents on the stack are restored to the cpu. second, every time a jump to subroutine (jsr) or a branch to subroutine (bsr) occurs, the return address is saved on the stack. the address is restored to the program counter after a return from subroutine (rts) instruction is executed. the cpu08 with its new stack manipulation instructions allows the user to pass parameters to the subroutine and store local or temporary values within the subroutine. two major benefits are derived from using the stack for parameters and temporary values: 1. a subroutine will allocate ram storage for its variables and release this memory when the subroutine is finished. therefore, global variables are not needed for these routines. this saves ram memory space. 2. the allocation of new local variables for each subroutine makes the subroutine recursive and reentrant. this allows the programmer to easily modularize his code. let's look at the stacking operation of the cpu05 and the cpu08. the stack is located in ram. since stacking occurs from high memory to low memory, the sp usually points to the highest ram memory address. both the cpu05 and the cpu08 reset the sp at $00ff. the cpu08 instruction set allows the programmer to move the stack out of page 0 memory if needed. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 16 when an interrupt occurs, the contents of all the cpu registers are pushed onto the stack, the interrupt vector is fetched, and the program begins execution at the start of the interrupt routine. the stack contents before and after an interrupt are shown in figure 2 . for the cpu08 to remain upward compatible with the cpu05, the h index register is not pushed onto the stack. note: if the h register is used in the interrupt service routine or if indexed addressing modes are used, the h register must be pushed onto the stack. this is accomplished by using the pshh instruction. before returning from the interrupt, the pulh instruction must be used to extract the h index register off the stack. stack before an interrupt call stack pointer ? ?? $00fe ?? $00ff stack after an interrupt call stack pointer ? $00fa condition code register $00fb accumulator a $00fc index register x $00fd program counter high $00fe program counter low $00ff figure 2. stack before and after an interrupt call f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note stack pointer AN1218 rev. 2 17 figure 3 illustrates the stack before and after a subroutine is called when the stack pointer is at $00ff. when a subroutine is called, the 16- bit program counter is pushed onto the stack and the execution of code begins at the start of the subroutine. the program counter is split into its 8-bit high and low bytes. if the values in the x register and the accumulator are needed within a subroutine, they will need to be saved somehow before the subroutine uses them. if using the cpu05, you would have to allocate global ram space for saving these cpu registers. your code would look something like that in figure 4 . stack before a subroutine call ?? $00fe stack pointer ? ?? $00ff stack after a subroutine call stack pointer ? index register x $00fd program counter high $00fe program counter low $00ff figure 3. stack before and after a subroutine call f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 18 * subroutine xx for cpu05 * start stx $50 ;4 store x away to ram sta $51 ;4 store a away to ram xx xx ;actual subroutine code xx xx ldx $50 ;3 load x from ram lda $51 ;3 load a from ram figure 4. cpu05 subroutine code the cpu05 code will use 14 cycles to store and load registers. also, two bytes of global ram space are allocated for this subroutine. if we were to use the cpu08, the code could utilize the stack. global ram space and six cycles would be saved. refer to figure 5 . * subroutine xx for cpu08 * start pshx ;2 push x onto stack pshh ;2 push h onto stack psha ;2 push a onto stack xx xx ;actual subroutine code xx xx pula ;2 pull a off of stack pulh ;2 pull h off of stack pulx ;2 pull x off of stack figure 5. cpu08 subroutine code the stack helps in efficiently utilizing parameters, local variables, and subroutine return values. parameters are variables that are passed to the subroutine. local variables are variables that are only used within the scope of the subroutine. a subroutine return value is the output of the subroutine. an example of a subroutine and its variables are given below in equation form: y = (x)3 if we were to write a subroutine that calculates the cube of the value x, x would be the parameter passed to the subroutine. y would be the subroutine return value, and any variable used to calculate y would be f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note stack pointer AN1218 rev. 2 19 a local variable. the stack of these complex subroutines follow the generalized structure shown in figure 6 . figure 6 shows the stack before the subroutine initialization, before entering the subroutine, and during the subroutine. the actual cube subroutine is written in the following section of code. a diagram of the stack during its execution is given within the code listing. code example refer to appendix d using the stack in a subroutine to compute a cube for an example of modular subroutine code that efficiently computes the cube of an 8-bit positive number. stack sp during subroutine ? $00f7 local variable 1 $00f8 local variable 2 $00f9 accumulator a $00fa index register x $00fb program counter high $00fc sp during subroutine ? program counter low $00fd sp before subroutine initialization return value $00fe ? passed parameter $00ff figure 6. stack structure of a complex subroutine f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 20 data movement why improve the movement of data in the cpu05? the most common cpu function is the transfer of data. most microcontroller-based systems spend the majority of their time moving data from one location to the other. many different addressing modes are used to access and transfer bytes of data. if there was a way to decrease the time it takes to transfer data, then the overall performance of the system would be improved. cpu05 moves data from one location to the next by first loading the accumulator with the byte from the transfer source. next, cpu05 stores the byte from the accumulator to the transfer's destination. in this manner all data must pass through the accumulator, thus making the accumulator a bottleneck in data movement. the movement of the contents of location $40 to location $60 with the cpu05 is illustrated in figure 7 . cpu08 provides the new mov instruction which bypasses the accumulator. using the mov instruction, the cpu is instructed to take the contents of the source location and directly place the data in the destination. this is illustrated in figure 8 . there are four different addressing modes special to the mov instruction. details of this instruction are given below. . lda $40 # $40 ?? # acca sta $60 # $60 ?? figure 7. accumulator as a bottleneck mov $40,$60 # $40 # $60 figure 8. no accumulator bottleneck f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note data movement AN1218 rev. 2 21 new move instruction, detail the new cpu08 move instruction is detailed below. examples for this instruction and all four of its addressing modes are given in appendix e new cpu08 mov instruction examples . the examples in appendix e new cpu08 mov instruction examples also compare the cpu05 and the cpu08 bus cycles and memory requirements for the algorithm to execute the movement of data. mov move operation: (m)destination ? (m)source description: moves a byte of data from a source address to a destination address. data is examined as it is moved, and condition codes are set. source data is not changed. internal registers (other than ccr) are not affected. there are four addressing modes for the mov instruction. a discussion of these modes was given in an earlier section. software techniques the mov command will cut cycle time and code space when moving data. the most obvious advantage of the mov instruction is when the configuration registers are being initialized along with other ram variables at the start of the program. code example a user wants to start his application one of two different ways. the user initializes the application on the mcu based on the logic level of port d bit 4. once the part is out of reset, it reads port d and moves data from rom into the ram configuration registers according to the logic level of bit 4. refer to appendix f cpu05 and cpu08 data movement code for code comparing the cpu05 and the cpu08. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 22 new branch instructions description six new unsigned branch instructions were added to the instruction set of the cpu08 to improve looping and table searching capabilities. these instructions are cbeq, cbeqa, cbeqx, dbnz, dbnza, and dbnzx. the cbeq instructions combine the compare (cmp and cpx) instructions and the branch if equal (beq) instruction . the dbnz instructions combine the decrement (dec, deca, and decx) instructions and the branch if not equal (bne) instruction. these new instructions improve cycle time and decrease code space. more detail is given below on each instruction. six new branch instructions, detail all the new cpu08 instructions that affect branching are listed below. examples for these instructions are given in appendix g new branch instruction examples . the examples in appendix g new branch instruction examples also compare the cpu05 and the cpu08 bus cycles and memory requirements for the algorithm to execute the branch. cbeq compare and branch if equal operation: a) - (m); pc ? (pc) + $0003 + rel if result is $00 for ix+ mode: (a) - (m); pc ? (pc) + $0002 + rel, if result is $00 description: cbeq compares the operand from memory with the accumulator and causes a branch if the result is zero. this function combines cmp and beq for faster table look-up routines. the addressing mode cbeq_ix+ compares the operand addressed by the 16-bit index register h:x to the accumulator and causes a branch if the result is zero. the 16-bit index register is then incremented regardless of whether a branch is taken. cbeq_ix1+ operates the same way except f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note new branch instructions AN1218 rev. 2 23 an 8-bit offset is added to the effective address of the operand. cbeqa compare and branch if equal operation: (a) - (m); pc ? (pc) + $0003 + rel if result is $00 description: cbeqa compares an immediate operand in memory with the accumulator and causes a branch if the result is zero. this instruction combines cmp and beq for faster table look-up routines. cbeqx compare and branch if equal operation: (ix) - (m); pc ? (pc) + $0003 + rel if result is $00 description: cbeqx compares an immediate operand in memory with the lower order index register x and causes a branch if the result is zero. this instruction combines cpx and beq for faster loop counter control. dbnz decrement and branch if not zero operation: m ? (m) - $01; pc ? (pc) + $0003 + rel, if result _ $00 for direct, ix1, and sp1 pc ? (pc) + $0002 + rel, if result _ $00 for ix description: dbnz subtracts one from the operand m in memory and causes a branch if the result is not zero. this instruction combines dec and bne for faster loop counter control. dbnza decrement and branch if not zero operation: a ? (a) - $01; pc ? (pc) + $0002 + rel, if result _ $00 description: dbnza subtracts one from the accumulator and causes a branch if the result is not zero. this instruction combines deca and bne for faster loop counter control. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 24 dbnzx decrement and branch if not zero operation: x ? (x) - $01; pc ? (pc) + $0002 + rel, if result _ $00 description: dbnzx subtracts one from the lower index register and causes a branch if the result is not zero. this instruction combines decx and bne for faster loop counter control. code example the use of these new instructions can cut cycle time in looping or counting routines. compare and branch routines can be used to search for specific values in tables or variable locations. decrement and branch routines can be used for keeping count in loops. the following piece of code shows how the compare and branch instruction searches a table for a match. as an example, let's say that you recently read in a table of 80 a/d data bytes. you would like to know if the signal was saturated above the rails of the a/d converter. you would then search the table for the value $ff. if found, your code would branch out and execute some control algorithm to attenuate the analog signal. refer to appendix h cpu05 and cpu08 search code for a comparison of cpu05 and cpu08 code. mathematical operations v bit, div, daa, and the nsa instruction new features and instructions added to the cpu08 have made some mathematical computations easier. the v bit is added to the ccr to support signed arithmetic. cpu08 has the capability of 16-bit division. the div instruction will divide a 16-bit dividend by an 8-bit divisor. for binary coded decimal operations, the cpu08 has a decimal adjust accumulator, daa, instruction and a nibble swap accumulator, nsa, instruction. signed math and signed branches the v bit in the ccr adds greater programming flexibility to the user. the addition of two's complement comparisons can aid in the branching f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note four new signed branch instructions, detail AN1218 rev. 2 25 operations of high level languages such as c. also, the representation of signed numbers and their operations can easily be computed. this can be especially helpful with digital signal processing algorithms and the proper storage of signed analog to digital readings. four new signed branch instructions, detail all the new cpu08 instructions that affect signed branching are listed below. examples for these instructions are given in appendix i new cpu08 signed branch instruction examples . bge branch if greater than or equal (signed operands) operation: pc ? (pc) + $0002 + rel, if (n ? v)=0, i.e., if (a) _ (m), ("signed" numbers) description: if the bge instruction is executed immediately after execution of any of the compare or subtract instructions, the branch will occur if and only if the two's complement number represented by the appropriate internal register (a, x, or h:x) was greater than or equal to the two's complement number represented by m. bgt branch if greater than (signed operands) operation: pc ? (pc) + $0002 + rel, if z+(n ? v)=0, i.e., if (a) > (m), ("signed" numbers) description: if the bgt instruction is executed immediately after execution of any of the compare or subtract instructions, the branch will occur if and only if the two's complement number represented by the appropriate internal register (a, x, or h:x) was greater than the two's complement number represented by m. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 26 ble branch if less than or equal (signed operands) operation: pc ? (pc) + $0002 + rel, if z+(n ? v)=1 i.e., if (a) _ (m), ("signed" numbers) description: if the ble instruction is executed immediately after execution of any of the compare or subtract instructions, the branch will occur if and only if the two's complement number represented by the appropriate internal register (a, x, or h:x) was less than or equal to the two's complement number represented by m. blt branch if less than (signed operands) operation: pc ? (pc) + $0002 + rel, if (n ? v)=1 i.e., if (a) < (m), ("signed" numbers) description: if the blt instruction is executed immediately after execution of any of the compare or subtract instructions, the branch will occur if and only if the two's complement number represented by the appropriate internal register (a, x, or h:x) was less than the two's complement number represented by m. new div instruction the divide instruction on the cpu08 does not require the lengthy code needed to divide numbers on the cpu05. a description of the divide instruction is given below. appendix j five miscellaneous cpu08 instructions including bcd, divide, and ccr operations shows a short example of using the new divide instruction. appendix k cpu08 averaging code illustrates an averaging routine implementing the divide instruction. div divide operation: (h:a) / x ? a; remainder ? h description: divides a 16-bit unsigned dividend contained in the concatenated registers h and a by an 8-bit divisor contained in index f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note four new signed branch instructions, detail AN1218 rev. 2 27 register x. the quotient is placed in the accumulator a, and the remainder is placed in the high order index register h. the divisor is left unchanged. new daa and the nsa instruction the decimal adjust accumulator, daa, and the nibble swap accumulator, nsa, are new instructions to help with binary coded decimal (bcd) operations. the daa instruction allows the user to adjust the accumulator so that the number represents a bcd number. swapping nibbles is needed for packing bcd numbers into memory. one use of bcd is data instrumentation. it is easier to store and manipulate these numbers in bcd rather than convert or decode numbers from hexadecimal. packing is used to store decimal numbers into memory. instead of one byte storing one decimal, the nsa instruction easily swaps nibbles in the accumulator so that two decimal numbers can be stored in one byte. appendix j five miscellaneous cpu08 instructions including bcd, divide, and ccr operations gives examples using the daa instruction and the nsa instruction. refer to appendi x l cpu08 bcd example code for an example of bcd code. daa decimal adjust accumulator operation: (a)10 description: adjusts the contents of the accumulator and the state of the ccr carry bit after binary coded decimal operations so that there is a correct bcd sum and an accurate carry indication. the state of the ccr half carry bit affects operation. nsa nibble swap accumulator operation: a ? (a[3:0]:a[7:4]) description: swaps upper and lower nibbles (4 bits) of the accumulator. this is used for more ef cient stor age and use of binary coded operands. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 28 new tap and tpa instructions the transfer accumulator to the condition code register, tap, and the transfer condition code register to accumulator, tpa, are new instructions to modify or manipulate the condition code register, ccr. these instructions are detailed below. code examples can be found in appendix j five miscellaneous cpu08 instructions including bcd, divide, and ccr operations . tap transfer accumulator to condition code register operation: ccr ? (a) description: transfers the contents of the accumulator to the condition code register. tpa transfer condition code register to accumulator operation: a ? (ccr) description: transfers the contents of the condition code register to the accumulator. instruction cycle improvements the cpu08 instruction set not only has new instructions but many of the old instructions are faster. the cpu08 gathers data in a pipeline fashion. instead of waiting for the instruction to be finished to gather the next opcode or operand, the cpu will fetch the next address byte during the execution of the current instruction. this pipelining overlaps execution of most instructions and thus increases the performance of the cpu08. a list of instructions that were improved is given in table 3 . please refer to the cpu08 opcode map for further details. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note instruction cycle improvements AN1218 rev. 2 29 table 3. instruction list (sheet 1 of 4) opcode mnemonic address mode hc05 cycles hc08 cycles adc ix 3 2 adc ix1 4 3 adc ix2 5 4 add ix 3 2 add ix1 4 3 add ix2 5 4 and ix 3 2 and ix1 4 3 and ix2 5 4 asr dir 5 4 asr ix 5 3 asr ix1 6 4 asra inh 3 1 asrx inh 3 1 bclr0 dir 5 4 bclr1 dir 5 4 bclr2 dir 5 4 bclr3 dir 5 4 bclr4 dir 5 4 bclr5 dir 5 4 bclr6 dir 5 4 bclr7 dir 5 4 bit ix 3 2 bit ix1 4 3 bit ix2 5 4 bset0 dir 5 4 bset1 dir 5 4 bset2 dir 5 4 bset3 dir 5 4 bset4 dir 5 4 bset5 dir 5 4 bset6 dir 5 4 bset7 dir 5 4 bsr rel 6 4 clc inh 2 1 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 30 clr dir 5 3 clr ix 5 2 clr ix1 6 3 clra inh 3 1 clrx inh 3 1 cmp ix 3 2 cmp ix1 4 3 cmp ix2 5 4 com dir 5 4 com ix 5 3 com ix1 6 4 coma inh 3 1 comx inh 3 1 cpx ix 3 2 cpx ix1 4 3 cpx ix2 5 4 dec dir 5 4 dec ix 5 3 dec ix1 6 4 deca inh 3 1 decx inh 3 1 eor ix 3 2 eor ix1 4 3 eor ix2 5 4 inc dir 5 4 inc ix 5 3 inc ix1 6 4 inca inh 3 1 incx inh 3 1 jsr dir 5 4 jsr ext 6 5 jsr ix 5 4 jsr ix1 6 5 jsr ix2 7 6 lda ix 3 2 table 3. instruction list (sheet 2 of 4) opcode mnemonic address mode hc05 cycles hc08 cycles f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note instruction cycle improvements AN1218 rev. 2 31 lda ix1 4 3 lda ix2 5 4 ldx ix 3 2 ldx ix1 4 3 ldx ix2 5 4 lsl dir 5 4 lsl ix 5 3 lsl ix1 6 4 lsla inh 3 1 lslx inh 3 1 lsr dir 5 4 lsr ix 5 3 lsr ix1 6 4 lsra inh 3 1 lsrx inh 3 1 mul inh 11 5 neg dir 5 4 neg ix 5 3 neg ix1 6 4 nega inh 3 1 negx inh 3 1 nop inh 2 1 ora ix 3 2 ora ix1 4 3 ora ix2 5 4 rol dir 5 4 rol ix 5 3 rol ix1 6 4 rola inh 3 1 rolx inh 3 1 ror dir 5 4 ror ix 5 3 ror ix1 6 4 rora inh 3 1 rorx inh 3 1 table 3. instruction list (sheet 3 of 4) opcode mnemonic address mode hc05 cycles hc08 cycles f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 32 rsp inh 2 1 rti inh 9 7 rts inh 6 4 sbc ix 3 2 sbc ix1 4 3 sbc ix2 5 4 sec inh 2 1 sta dir 4 3 sta ext 5 4 sta ix 4 2 sta ix1 5 3 sta ix2 6 4 stop inh 2 1 stx dir 4 3 stx ext 5 4 stx ix 4 2 stx ix1 5 3 stx ix2 6 4 sub ix 3 2 sub ix1 4 3 sub ix2 5 4 swi inh 10 9 tax inh 2 1 tst dir 4 3 tst ix 4 2 tst ix1 5 3 tsta inh 3 1 tstx inh 3 1 txa inh 2 1 wait inh 2 1 table 3. instruction list (sheet 4 of 4) opcode mnemonic address mode hc05 cycles hc08 cycles f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note conclusion AN1218 rev. 2 33 conclusion this application note has covered the differences between the hc05 and the hc08 cpu architecture. please refer to the m68hc05 applications guide for further study of the cpu05. the cpu08 reference manual is a valuable resource for studying the cpu08 in more detail. please consult your local freescale sales office or your authorized freescale distributor for applications support, literature, and specific part information. the mcu bbs is also available with free software for use with hc05 and hc08 mcus. the bbs number is (512) 891-3733. the code examples used in this application note can be found on the bbs. the file name is hc08opt.arc. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 34 appendix a new cpu08 indexing instruction examples ************************************************************ * * file : index.asm * description : * shows examples for new cpu08 indexing * instructions - aix, clrh, cphx, ldhx, sthx * not all addressing modes are shown. * note : please consult the cpu08 reference manual * for further details on these instructions * code is written for educational * purposes only * ************************************************************** org $200 ***** aix - add immediate to index register start ldhx #$1010 ; h:x ? $1010 aix #-10 ; h:x = $1010 + (-$10) ; = $1000 ***** clrh - clear index high ldhx #$1290 ; h:x ? $1290 clrh ; h:x ? $0090 ***** cphx - compare 16-bit index register ldhx #$1290 ; h:x ? $1290 ; ccr = %0110,1000 ; ccr before cphx, z=0 cphx #$1290 ; h:x ? $1290 ; ccr = %0110,1010 ; ccr after cphx, z=1 ***** ldhx - load 16-bit index register ldhx #$1290 ; h:x ? $1290 ***** sthx - store 16-bit index register ldhx #$1290 ; h:x ? $1290 sthx $50 ; ($50) ? (h:x) ; ($50) ? $12 ; ($51) ? $90 done nop bra done ***** initialize the reset vector org $fffe dw start f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note appendix b cpu05 and cpu08 512-byte table indexing code AN1218 rev. 2 35 appendix b cpu05 and cpu08 512-byte table indexing code ************************************************************ * * file : indexx.asm * description : * the following code illustrates the * different instructions used to address * a 512 byte table in memory. hc05 and hc08 * code is compared. * notes: comments to the right of some instructions * give numbers. * cpu05 - 1st # is cpu05 cycle count * 2nd # is instruction byte count * cpu08 - 1st # is cpu08 cycle count * 2nd # is instruction byte count * please consult the cpu08 reference manual * for further details on these instructions * code is written for educational * purposes only * ************************************************************ * for the purpose of this example, the table address * will be predefined in ram. * tbl_a = $120 tbl_st0 equ $400 ; start of table, section 0 tbl_st1 equ tbl_st0+256t ; start of table, section 1 org $50 ; start of ram variables tbl_a rmb 2 ; address for table to be ; accessed by the code org $200 ***** address a 512 byte table with the index register ***** the table starts at $400 and ends at $5ff f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 36 ********************************* * hc05 code * * cpu05 has to address the table in a section-like * fashion. section 0 is between $400 and $4ff. * section 1 is between $500 and $5ff. * the 16-bit address is stored in ram location tbl_a. * this is the offset to the table starting * at $400, tbl_st0. * example: address is $520 = $400 + $120 * tbl_a = $01 * tbl_a+1 = $20 start ldx tbl_a+1 ;3,2 x ? (tbl_a+1) lda tbl_a ;3,2 a ? (tbl_a) beq tbl0 ;3,2 branch to section 0 if 0 lda tbl_st1,x ;5,3 a ? (x+tbl_st1) bra next ;3,2 branch when done to ; the cpu08 example tbl0 lda tbl_st0,x ;5,3 a ? (x+tbl_st0) * total # cpu05 cycles = 17 (max) * total # bytes = 11 (max) ********************************* ********************************* * hc08 code * * cpu08 has full 16-bit indexed addressing so the * table address is loaded from tbl_a in ram. no * memory table sectioning is needed. next ldhx tbl_a ;4,2 h:x ? (tbl_a) lda tbl_st0,x ;4,3 a ? (x+tbl_st0) * total # cpu08 cycles = 8 * total # bytes = 5 ********************************* done nop bra done ***** initialize the reset vector org $fffe dw start f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note appendix c new cpu08 stack pointer instructions AN1218 rev. 2 37 appendix c new cpu08 stack pointer instructions ************************************************************ * * file : sp.asm * description : * shows examples for new cpu08 stack pointer * instructions - ais, psha, pshh, pshx * pula, pulh, pulx, tsx, txs * not all addressing modes are shown. * note : please consult the cpu08 reference manual * for further details on these instructions * code is written for educational * purposes only * ************************************************************ org $200 ***** ais - add immediate to stack pointer * sp is predefined at $0fe0 start ais #$1f ; sp ? $0fe0 + $1f ; sp = $0fff ***** psha - push accumulator onto stack * sp is predefined at $0fff * a = $80 psha ; ($0fff) ? $80 ; sp ? sp-$01 ; sp = $0ffe ***** pshh - push index register h onto stack * sp is predefined at $0ffe * h:x = $2050 pshh ; ($0ffe) ? $20 ; sp ? sp-$01 ; sp = $0ffd ***** pshx - push index register x onto stack * sp is predefined at $0ffd * h:x = $2050 pshx ; ($0ffd) = $50 ; sp ? sp-$01 ; sp = $0ffc f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 38 ***** pulx - pull index register x from stack * sp is predefined at $0ffc * $0ffd = $50 * h:x = $0000 pulx ; sp ? sp+$01 ; sp = $0ffd ; x ? ($0ffd) ; h:x = $0050 ***** pulh - pull index register h from stack * sp is predefined at $0ffd * $0ffe = $20 * h:x = $0050 pulh ; sp ? sp+$01 ; sp = $0ffe ; h ? ($0ffe) ; h:x = $2050 ***** pula - pull accumulator from stack * sp is predefined at $0ffe * $0fff = $80 * a = $00 pula ; sp ? sp+$01 ; sp = $0fff ; a ? ($0fff) ; a = $80 ***** tsx - transfer stack pointer to index register * sp is predefined at $0ff5 * h:x = $1290 tsx ; h:x ? sp+$01 ; h:x = $0ff6 ***** txs - transfer index register to stack pointer * sp is predefined at $0ff5 * h:x = $1290 txs ; sp ? h:x-$01 ; sp = $128f done nop bra done ***** initialize the reset vector org $fffe dw start f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note appendix d using the stack in a subroutine to compute a cube AN1218 rev. 2 39 appendix d using the stack in a subroutine to compute a cube ************************************************************ * * file : cube.asm * description : * this program takes an 8-bit positive * number, x_in, and cubes it. the answer, * y_in, is in a 24-bit format. * this program also illustrates the * value of using the stack for complex * subroutines that use parameter passing, * local variables, and return values. * stack description: * given below is a diagram of the stack * during the subroutine * the numbers on the right specify the * number of bytes above the stack pointer * * ------------------------- * sp ? ?? * var1 1 * var2 2 * a 3 * h 4 * x 5 * pc_high 6 * pc_low 7 * y_high 8 * y_med 9 * y_low 10 * x_in 11 * ------------------------- * * note : please consult the cpu08 reference manual * for further hc08 instruction details * code is written for educational * purposes only * ************************************************************ org $80 x_in rmb 1 ;8-bit number to be cubed org $200 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 40 ***** load up stack before entering the subroutine * stack is given the 8-bit number to be cubed, x_in * next, 3 bytes must be made available to the stack * for the 24 bit output of the routine * 3 pushes are made to illustrate this point start lda x_in ;a ? (x_in) psha ;push parameter x_in onto stack clra ;zero must be pushed on stack ; allocation for return answer psha ;push y_low byte onto stack psha ;push y_med byte onto stack psha ;push y_high byte onto stack ***** jump to the cube subroutine jsr cube ;jump sub to cube, y = x_in^3 ***** when subroutine is over, reset stack pointer to original * location. pull the answers off the stack when needed. ais #$04 ;sp ? (sp) + $04 bra done ;branch to the end of this ;example ***** cube subroutine ***** given x_in, find y = x^3 * save x,h, and a on stack * decrement stack for 2 bytes cube pshx ;push x onto stack pshh ;push h onto stack psha ;push a onto stack ais #-2 ;decrement stack for local var * run the math routine * square x_in, answer is x:a lda 11t,sp ;a = x_in ldx 11t,sp ;x = x_in mul ;x:a = (x)*(a) * store away the high byte answer, x, to var1 stx 1,sp ;store high answ to var1 * multiply 16 bit result by x_in * multiply x_in by low byte of 16-bit square ldx 11t,sp ;x = x_in mul ;x:a = (x)*(a) f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note appendix d using the stack in a subroutine to compute a cube AN1218 rev. 2 41 * store away low byte of 16-bit result * to y_low * store high byte of 16-bit result to var2 sta 10t,sp ;store low answ to y_low stx 2,sp ;store high answ to var2 * multiply high byte of 16-bit result by x_in lda 11t,sp ;a ? x_in ldx 1,sp ;load x with var1 mul ;x:a = x_in * var1 * store high byte of answer to y_high stx 8t,sp ;store high byte to y_high * add var2 to the low byte answer to get y_med * if there is a carry, add one bit to y_high add 2t,sp ;a = var2 + a bcs cs ;branch if c bit set in ccr bra fin ;c bit is 0, branch to fin cs inc 8t,sp ;add 1 to y_high fin sta 9t,sp ;store a to y_med * save x,h, and a on stack * increment stack for 2 bytes * restore x,h, and a * return from the subroutine ais #$02 pula pulh pulx rts done nop bra done ***** initialize the reset vector org $fffe dw start f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 42 appendix e new cpu08 mov instruction examples ************************************************************ * * file : move.asm * description : * shows examples for the mov instruction * all four addressing modes are illustrated * 05 and 08 code is compared * notes: comments to the right of some instructions * give numbers. * cpu05 - 1st # is cpu05 cycle count * 2nd # is instruction byte count * cpu08 - 1st # is cpu08 cycle count * 2nd # is instruction byte count * please consult the cpu08 reference manual * for further details on these instructions * code is written for educational * purposes only * ************************************************************ org $200 ***** move immediate value to direct memory location * hc05 * start lda #$30 ;2,2 a ? $30 sta $80 ;4,2 ($80) ? (a) * hc08 * mov #$30,$80 ;4,3 ($80) ? $30 * total cpu05 cycles, bytes = 6,4 * total cpu08 cycles, bytes = 4,3 ***** move direct mem value to direct mem location * hc05 * lda $80 ;3,2 a ? ($80) sta $90 ;4,2 ($90) ? (a) * hc08 * mov $80,$90 ;5,3 ($90) ? ($80) * total cpu05 cycles, bytes = 7,4 * total cpu08 cycles, bytes = 5,3 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note appendix e new cpu08 mov instruction examples AN1218 rev. 2 43 ***** move contents of indexed to direct mem location, post inc xreg * hc05 * ldx #$80 ; x ? $80 lda ,x ;3,1 a ? (x) sta $90 ;4,2 ($90) ? (a) incx ;3,1 x ? x + 1 * hc08 * ldx #$80 ; x ? $80 mov x+,$90 ;4,2 ($90) ? (x) ; x ? x + 1 * total cpu05 cycles, bytes = 10,4 * total cpu08 cycles, bytes = 4,2 ***** move direct mem contents to indexed location, post inc xreg * hc05 * ldx #$90 ; x ? $90 lda $80 ;3,2 a ? ($80) sta ,x ;4,1 (x) ? (a) incx ;3,1 x ? x + 1 * hc08 * ldx #$90 ; x ? $90 mov $80,x+ ;4,2 (x) ? ($80) ; x ? x + 1 * total cpu05 cycles, bytes = 10,4 * total cpu08 cycles, bytes = 4,2 ***** initialize the reset vector org $fffe dw start f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 44 appendix f cpu05 and cpu08 data movement code ************************************************************ * * file : movex.asm * description : * a user wants to start an application one of * two different ways. the user sets the * application on the mcu by the logic level * of port d, bit 3. once out of reset, the * mcu reads port d and moves data from rom * into the ram configuration registers * according to the logic level of bit 3. * notes: comments to the right of some instructions * give numbers. * cpu05 - 1st # is cpu05 cycle count * 2nd # is instruction byte count * cpu08 - 1st # is cpu08 cycle count * 2nd # is instruction byte count * please consult the cpu08 reference manual * for further details on these instructions * code is written for educational * purposes only * ************************************************************ * for the purpose of this example, we will be using * random ctrl registers for the code. they are listed * below in an equate table tbl equ $1000 ; start of table portd equ $03 ; port d data register portadr equ $04 ; port a data direction register portbdr equ $05 ; port b data direction register spictrl equ $0a ; spi control register scictrl equ $0e ; sci control register timctrl equ $12 ; timer control register org $200 ***** if bit 3 = 0 when read, then the table * starts at $1000 * if bit 3 = 1 when read, then the table * starts at $1008 ********************************* * hc05 code * start05 lda portd ;3,2 a ? (portd) and #$08 ;2,2 clear a except bit 3 ; a = 0 or 8 tax ;2,1 x ? (a) ; set the offset of x f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note appendix f cpu05 and cpu08 data movement code AN1218 rev. 2 45 lda tbl,x ;5,3 a ? (tbl+x) sta portadr ;4,2 (portadr) ? (a) incx ;3,1 x ? x + 1 lda tbl,x ;5,3 a ? (tbl+x) sta portbdr ;4,2 (portbdr) ? (a) incx ;3,1 x ? x + 1 lda tbl,x ;5,3 a ? (tbl+x) sta spictrl ;4,2 (spictrl) ? (a) incx ;3,1 x ? x + 1 lda tbl,x ;5,3 a ? (tbl+x) sta scictrl ;4,2 (scictrl) ? (a) incx ;3,1 x ? x + 1 lda tbl,x ;5,3 a ? (tbl+x) sta timctrl ;4,2 (timctrl) ? (a) * total # cpu05 cycles = 64 * total # bytes = 34 ********************************* ********************************* * hc08 code * start08 ldhx #tbl ;3,3 h:x ? tbl lda portd ;3,2 a ? (portd) and #$08 ;2,2 clear a except bit 3 ; a = 0 or 8 tax ;1,1 x ? (a) ; set the offset of x mov x+,portadr ;4,2 (portadr) ? (h:x) ; x ? x + 1 mov x+,portbdr ;4,2 (portbdr) ? (h:x) ; x ? x + 1 mov x+,spictrl ;4,2 (spictrl) ? (h:x) ; x ? x + 1 mov x+,scictrl ;4,2 (scictrl) ? (h:x) ; x ? x + 1 mov x+,timctrl ;4,2 (timctrl) ? (h:x) ; x ? x + 1 * total # cpu08 cycles = 29 * total # bytes = 18 ********************************* done nop bra done ***** initialize the reset vector org $fffe dw start05 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 46 appendix g new branch instruction examples ************************************************************ * * file : branch.asm * description : * shows examples for new cpu08 branch * instructions - cbeq, cbeqa, cbeqx * dbnz, dbnza, dbnzx * notes: comments to the right of some instructions * give numbers. * cpu05 - 1st # is cpu05 cycle count * 2nd # is instruction byte count * cpu08 - 1st # is cpu08 cycle count * 2nd # is instruction byte count * please consult the cpu08 reference manual * for further details on these instructions * code is written for educational * purposes only * ************************************************************ org $200 ***** cbeq - compare and branch if equal, direct * a is predefined at $40 * memory location $80 contains $40 * hc05 code * lpa cmp $80 ;3,2 (a) - ($80) beq lp1 ;3,2 if (a) = ($80) then ; branch to lp1 bra lpa ; go to lpa, try again! * hc08 code * lp1 cbeq $80,lpb ;5,3 if (a)-($80)=0, ; then branch to lpb bra lp1 ; go to lp1 * total cpu05 cycles, bytes = 6,4 * total cpu08 cycles, bytes = 5,3 ***** cbeqa - compare and branch if equal, immediate * a is predefined at $50 * hc05 code * lpb cmp #$50 ;2,2 (a) - $50 beq lp2 ;3,2 if (a) = $50, then lp2 bra lpb ; go to lpb f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note appendix g new branch instruction examples AN1218 rev. 2 47 * hc08 code * lp2 cbeqa #$50,lpc ;4,3 if #$50 = (a), then lpc bra lp2 ; go to lp2 * total cpu05 cycles, bytes = 5,4 * total cpu08 cycles, bytes = 4,3 ***** cbeqx - compare and branch if equal, index * index register x is predefined at $60 * hc05 code * lpc cpx #$60 ;2,2 x - $60 beq lp3 ;3,2 if x = $60, then lp3 bra lpc ; go to lpc * hc08 code * lp3 cbeqx #$60,lpd ;4,3 if x = $60, then lpd bra lp3 ; go to lp3 * total cpu05 cycles, bytes = 5,4 * total cpu08 cycles, bytes = 4,3 ***** dbnz - decrement and branch if not zero * hc05 code * * memory location $a0 is predefined at $08 lpd nop ; used here to represent any ; number of instructions dec $a0 ;5,2 decrement ($a0) bne lpd ;3,2 if ($a0) not zero, then lpd * hc08 code * * memory location $a0 is predefined at $08 lp4 nop ; used here to represent any ; number of instructions dbnz $a0,lp4 ;5,3 ($a0) = ($a0) - 1 ; if ($a0) not zero, then lp4 * total cpu05 cycles, bytes = 8,4 * total cpu08 cycles, bytes = 5,3 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 48 ***** dbnza - decrement acca and branch if not zero * hc05 code * * a is predefined at $06 lpe nop ; used here to represent any ; number of instructions deca ;3,1 (a) = (a) - 1 bne lpe ;3,2 if (a) not zero, then lpe * hc08 code * * a is predefined at $06 lp5 nop ; used here to represent any ; number of instructions dbnza lp5 ;3,2 (a) = (a) - 1 ; if (a) not zero, then lp5 * total cpu05 cycles, bytes = 6,3 * total cpu08 cycles, bytes = 3,2 ***** dbnzx - decrement x and branch if not zero * hc05 code * * index register x is predefined at $04 lpf nop ; used here to represent any ; number of instructions decx ;3,1 (x) = (x) - 1 bne lpf ;3,2 if (x) not zero, then lpf * hc08 code * * index register x is predefined at $04 lp6 nop ; used here to represent any ; number of instructions dbnzx lp6 ;3,2 (x) = (x) - 1 ; if (x) not zero, then lp6 * total cpu05 cycles, bytes = 6,3 * total cpu08 cycles, bytes = 3,2 done nop bra done ***** initialize the reset vector org $fffe dw lpa f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note appendix h cpu05 and cpu08 search code AN1218 rev. 2 49 appendix h cpu05 and cpu08 search code ************************************************************ * * file : branchx.asm * description : * this code shows an example of using branch * algorithms to search for a number in a * table. the code will search for $ff in * a table. this would signify that in a * table of a/d values, an a/d reading * was saturated. * notes: comments to the right of some instructions * give numbers. * cpu05 - 1st # is cpu05 cycle count * 2nd # is instruction byte count * cpu08 - 1st # is cpu08 cycle count * 2nd # is instruction byte count * please consult the cpu08 reference manual * for further details on these instructions * code is written for educational * purposes only * ************************************************************ table equ $400 ; starting address of the ; a/d table org $50 tbl_len rmb 1 ; memory value containing ; the number of values in ; a the a/d table org $200 ***** search for $ff (saturation) in a table of a/d values * tbl_len is predefined at $08 for this example * therefore the table is defined from $400 to $407 * the values given for the total # of cycles and bytes * reflect an absolute count with no looping involved * an accurate account of the cycle count would involve * the table length and whether or not a comparison * was made. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 50 * hc05 code * srch ldx tbl_len ;3,2 x ? (tbl_len) loop3 lda table-1,x ;5,3 a ? (table-1+x) cmp #$ff ;2,2 (a) - $ff beq next ;3,2 if z=1, then goto next ; this signifies that a ; saturation value has been ; found in the table decx ;3,1 x ? x -1 bne loop3 ;3,2 if z=0, then goto loop3 ; go look at another value ; in the table * total # cpu05 cycles = 19 * total # bytes = 12 * hc08 code * next ldx tbl_len ;3,2 x ? (tbl_len) loop4 lda table-1,x ;4,3 a ? (table-1+x) cbeqa #$ff,done ;4,3 (a) - $ff ; if z=1, then goto done ; this signifies that a ; saturation value has been ; found in the table dbnzx loop4 ;3,2 x ? x -1 ; if z=0, then goto loop4 ; go look at another value ; in the table * total # cpu08 cycles = 14 * total # bytes = 10 done nop bra done ***** initialize the reset vector org $fffe dw srch f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note appendix i new cpu08 signed branch instruction examples AN1218 rev. 2 51 appendix i new cpu08 signed branch instruction examples ************************************************************ * * file : signbra.asm * description : * shows examples for new cpu08 signed branch * instructions - bge, bgt, ble, blt * the examples demonstrate two's complement * math with branching. * note : please consult the cpu08 reference manual * for further details on these instructions * code is written for educational * purposes only * ************************************************************ org $200 ***** bge - branch if greater than or equal * a is predefined at $ff lp_bge cmp #$ff ; (a) - $ff, [ -1 - (-1) ] bge lp_bgt ; if a >= $ff, then ; branch to lp_bgt bra lp_bge ; go to lp_bge ***** bgt - branch if greater than * a is predefined at $07 lp_bgt cmp #$ff ; (a) - $ff, [ 7 - (-1) ] bgt lp_ble ; if a > $ff, then ; branch to lp_ble bra lp_bgt ; go to lp_bgt ***** ble - branch if less than or equal * a is predefined at $ff lp_ble cmp #$ff ; (a) - $ff, [ -1 - (-1) ] ble lp_blt ; if a <= $ff, then ; branch to lp_blt bra lp_ble ; go to lp_ble ***** blt - branch if less than * a is predefined at $ff lp_blt cmp #$07 ; $ff - $07, [ -1 - (7) ] blt done ; if a < $ff, then ; branch to done bra lp_blt ; go to lp_blt done nop bra done ***** initialize the reset vector org $fffe dw lp_bge f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 52 appendix j five miscellaneous cpu08 instructions including bcd, divide, and ccr operations ************************************************************ * * file : miscinst.asm * description : * shows examples for 5 misc cpu08 instructions * that include bcd, divide, and ccr operations * they are daa, nsa, div, tap, tpa * notes: comments to the right of some instructions * give numbers. * cpu05 - 1st # is cpu05 cycle count * 2nd # is instruction byte count * cpu08 - 1st # is cpu08 cycle count * 2nd # is instruction byte count * please consult the cpu08 reference manual * for further details on these instructions * code is written for educational * purposes only * ************************************************************ org $200 ***** daa - decimal adjust accumulator start lda #$26 ; a ? $26, a bcd # add #$37 ; a ? $37 + (a) ; a = $5d, a hex # daa ; (a) = 63 = (26 + 37) ; the hex #, 5d, has been ; adjusted to the bcd #, 63 ***** nsa - nibble swap accumulator * a is predefined at $37 * when finished a will be at $73 * hc05 code * tax ;2,1 x ? (a) rolx ;3,1 rotate left x rola ;3,1 rotate left a rolx ;3,1 rotate left x rola ;3,1 rotate left a rolx ;3,1 rotate left x rola ;3,1 rotate left a rolx ;3,1 rotate left x rola ;3,1 rotate left a f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note appendix j five miscellaneous cpu08 instructions including bcd, divide, and ccr operations AN1218 rev. 2 53 * hc08 code * nsa ;3,1 swap the nibbles of a * total cpu05 cycles, bytes = 26,9 * total cpu08 cycles, bytes = 3,1 ***** div - divide 16 bit by 8 bit * the immediate addressing mode is used to load the registers * to illustrate the components needed to execute * a div instruction. ldhx #$0200 ; h ? $02 ldx #$80 ; x ? $80 lda #$00 ; a ? $00 div ; h:a / x = a rem h ; answer is $04 rem 0 ***** tap - transfer accumulator to ccr * a is predefined at $e2 * ccr = %0110,0000 tap ; ccr ? (a) ; ccr = %1110,0010 ***** tpa - transfer ccr to accumulator * a is predefined at $00 * ccr = %1110,0010 tpa ; a ? (ccr) ; a = $e2 done nop bra done ***** initialize the reset vector org $fffe dw start f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note AN1218 rev. 2 54 appendix k cpu08 averaging code ************************************************************ * file : average.asm * description : * this code demonstrates an average routine * showing the use of the cpu08's div inst. * 8-bit values are read from a table in * memory and the average of those numbers * is computed. * notes: please consult the cpu08 reference manual * for further details on these instructions * code is written for educational * purposes only ************************************************************ tbl_str equ $400 ; starting address of ; table in memory org $50 length rmb 1 ; length of table tot_h rmb 1 ; high byte of total tot_l rmb 1 ; low byte of total org $200 * the length of this table is predefined as 3 for * this example * the values in the table start at $401 * $401 = 50 * $402 = 60 * $403 = 70 start clr tot_h ; clear tot_h in mem clr tot_l ; clear tot_l in mem ldx length ; x ? length of table next lda tbl_str,x ; a ? (x+tbl_str) add tot_l ; a ? (a)+(tot_l) sta tot_l ; tot_l ? (a) bcs cs ; if carry bit is set, ; branch to cs bra next2 ; branch to next table entry cs inc tot_h ; inc the high byte of total next2 dbnzx next ; dec x, if x not 0, then ; branch to next table entry ldhx tot_h ; h ? high byte of dividend txa ; a ? low byte of dividend ldx length ; x ? load divisor div ; h:a / x ; answer in a with h rem * answer can be found in a, remainder in h * answer is equal to $60 with no remainder done nop bra done ***** initialize the reset vector org $fffe dw start f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
application note appendix l cpu08 bcd example code AN1218 rev. 2 55 appendix l cpu08 bcd example code ************************************************************ * * file : bcd.asm * description : * this code demonstrates a bcd routine to * be used on the cpu08. * two 16-bit bcd numbers are added together * and the result is 16-bit bcd number * bcd1 + bcd2 = bcdt * notes: please consult the cpu08 reference manual * for further details on these instructions * code is written for educational * purposes only * ************************************************************ org $50 bcd1_h rmb 1 ; high byte of bcd #1 bcd1_l rmb 1 ; low byte of bcd #1 bcd2_h rmb 1 ; high byte of bcd #2 bcd2_l rmb 1 ; low byte of bcd #2 bcdt_h rmb 1 ; high byte of bcd total bcdt_l rmb 1 ; low byte of bcd total org $200 * predefine values for the example * bcd1 = 150, bcd1_h = 01 & bcd1_l = 50 * bcd2 = 250, bcd2_h = 02 & bcd2_l = 50 * first, add the low bytes of the 16-bit bcd #s start lda bcd1_l ; a ? (bcd #1 low byte) add bcd2_l ; a ? (a)+(bcd #2 ; low byte) daa ; decimal adjust accumulator sta bcdt_l ; store away result to total low * second, add the high bytes of the 16-bit bcd #s * add the carry bit from the previous addition lda bcd1_h ; a ? (bcd #1 high byte) adc bcd2_h ; a ? (a)+(bcd #2 ; high byte)+c daa ; decimal adjust accumulator sta bcdt_h ; store away result to total high * answer is in bcdt_h and bcdt_l * bcdt_h = 04 * bcdt_l = 00 done nop bra done ***** initialize the reset vector org $fffe dw start f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
non-disclosure agreement required application note f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .


▲Up To Search▲   

 
Price & Availability of AN1218

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X